package com.tfswx.ruleengine.compute.gzjy.felfunc;

import com.greenpineyu.fel.context.FelContext;
import com.tfswx.ruleengine.compute.gzjy.BaseFn;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ricky
 * @date 2021/5/26
 */
public class Substring extends BaseFn {
    @Override
    public Object call(Object[] arguments, FelContext context) {
        String str = String.valueOf(arguments[0]);
        int len = str.length();
        int beginIndex = Integer.parseInt(String.valueOf(arguments[1]));
        int endIndex = len;
        if (arguments.length == 3) {
            endIndex = Integer.parseInt(String.valueOf(arguments[2]));
        }
        if (endIndex > len) {
            endIndex = len;
        }
        return str.substring(beginIndex, endIndex);
    }

    @Override
    public String getZwmc() {
        return "截取字符串";
    }

    @Override
    public boolean isExport() {
        return true;
    }

    @Override
    public boolean isBuiltIn() {
        return true;
    }

    @Override
    public List<Param> getParams() {
        List<Param> params = new ArrayList<>();
        params.add(new Param("string"));
        params.add(new Param("beginIndex"));
        params.add(new Param("endIndex", "", false));
        return params;
    }

    @Override
    public String getName() {
        return "substring";
    }
}
